package org.dromara.common.tenant.filter;

/**
 * dubbo租户信息传递
 * @author: gxp
 * @desc: todo
 * @create: 2024-09-27 16:37
 **/
import com.alibaba.dubbo.rpc.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.dromara.common.core.constant.TenantConstants;
import org.dromara.common.tenant.helper.TenantHelper;

@Slf4j
@Activate(group = {CommonConstants.PROVIDER})
public class DubboTenantFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        try {
            String tenantId = RpcContext.getContext().getAttachment(TenantConstants.HEAD_TENANT_ID);
            TenantHelper.setDynamic(tenantId);
            Result result = invoker.invoke(invocation);
            return result;
        }finally {
            TenantHelper.clearDynamic();
        }
    }
}
